package com.buffer.springai.service;

import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import com.buffer.springai.config.NacosConfig;
import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;

@Service
public class SpringAIUnStreamService {

    @Resource
    private NacosConfig nacosConfig;

    // 非流式输出
    public String askAIWithoutStream(String content) {
        ChatCompletion chatCompletion = null;
        OpenAIClient client = OpenAIOkHttpClient.builder()
                .apiKey(nacosConfig.getApiKey())
                .baseUrl(nacosConfig.getBaseUrl())
                .build();

        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                .addUserMessage(content)
                .model("qwen-flash")
                .build();

        try {
            chatCompletion = client.chat().completions().create(params);
            System.out.println(chatCompletion);
        } catch (Exception e) {
            System.err.println("Error occurred: " + e.getMessage());
            e.printStackTrace();
        }

        return chatCompletion == null ? "" : chatCompletion.choices().get(0).message().content().get();
    }
    
}
